/**
 * by gsunwu@foxmail.com
 * 入口文件
 */


import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        /*文件转为mac帧：模拟发送数据*/
        file2mac();
        /*mac帧还原为文件：模拟接受数据*/
        mac2File();


    }

    /**
     * mac帧还原为文件：模拟接受数据
     * @throws IOException
     */
    private static void mac2File() throws IOException {
        /*首先删除中间文件*/

        File midFile = new File(Config.OUTPUT);
        if(midFile.isFile()){
            midFile.delete();
        }
        System.out.println("---------------------------------------------");
        System.out.println("------------------MAC帧转为文件----------------");
        System.out.println("---------------------------------------------");
        File f = new File(Config.MACFILE);
        RandomAccessFile racf = new RandomAccessFile(f, "r");
        if(Config.DEBUG) System.out.println("中间文件总长度:"+racf.length());


        int psize = 7904;
        Trans t = new Trans();
        /*大文件拆分为多个包*/
        int off = 0;
        while (off < racf.length()) {
            if(Config.DEBUG) System.out.println("------------ 结果分隔符 -----------------");
            byte[] content = new byte[psize];
            /*防止越界*/
            racf.seek(off);
            racf.read(content, 0, psize-1);

            System.out.println("中间文件长度:" + content.length);
            String middle = new String(content);
            String data = middle.substring(112, psize-32);
            System.out.println("实际内容(二进制)：\r\n"+data);
            System.out.println("实际内容(十六进制)："+Utils.binaryToHex(data));
            System.out.println("实际内容（正常文本）: "+Utils.hex2String(Utils.binaryToHex(data)));
            /*将转换的帧写入文件*/
            Utils.WriteDataToFile(Utils.hex2String(Utils.binaryToHex(data)),"OUTPUT");
            off += psize;

        }
    }

    /**
     * 文件转为mac帧：模拟发送数据
     * @throws IOException
     */
    private static void file2mac() throws IOException {

        System.out.println("---------------------------------------------");
        System.out.println("------------文件转为MAC帧----------------------");
        System.out.println("---------------------------------------------");
        /*首先删除中间文件*/
        File midFile = new File(Config.MACFILE);
        if(midFile.isFile()){
            midFile.delete();
        }
        File f = new File(Config.INPUT);
        RandomAccessFile racf = new RandomAccessFile(f, "r");

        if(Config.DEBUG) System.out.println("文件总长度（byte）:"+racf.length());


        int psize = 970;
        Trans t = new Trans();
        /*大文件拆分为多个包*/
        int off = 0;
        while (off <= racf.length()) {
            byte[] content = new byte[psize];
            /*防止越界*/
            racf.seek(off);
            racf.read(content, 0, psize);


            t.setStr(new String(content));
            String mac = t.getMac();
            System.out.println("实际内容: " + new String(content));
            System.out.println("帧内容长度(二进制):" + mac.length());
            System.out.println("帧内容(二进制):" + mac);
            System.out.println("帧内容长度(十六进制):" + Utils.binaryToHex(mac).length());
            System.out.println("帧内容(十六进制):" + Utils.binaryToHex(mac));
            /*将转换的帧写入文件*/
//            t.output();

            if(Config.DEBUG) System.out.println("-------------包分割符-----------------");

            Utils.WriteDataToFile(mac,"MID");
            off += psize;
        }
    }
}